//
//  WK_StockChartViewController.m
//  BTC-Kline
//
//  Created by Canny on 2018/11/19.
//  Copyright © 2018年 yate1996. All rights reserved.
//

#import "WK_StockChartViewController.h"
#import "WKStockChartTableViewCell.h"
#import "WKStockHeaderTableViewCell.h"
#import "WKStockOtherTableViewCell.h"
#import "WKStockFooterView.h"
#import "WKStock_Chart_Detail_Type_Section_View.h"
#import "WKStockDepthTableViewCell.h"
#import "WKStockDealTableViewCell.h"
#import "WKStockIntrolTableViewCell.h"

@interface WK_StockChartViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSMutableArray * sectionMarr;

@property(nonatomic, strong)WKStockChartTableViewCell *chart_Cell;

@property(nonatomic, strong)WKStockFooterView * footerView;

@property(nonatomic, assign)NSInteger type;



@end

@implementation WK_StockChartViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SRWebSocketDidReceiveMsg:) name:Kline_kWebSocketdidReceiveMessageNote object:nil];
}

- (void)SRWebSocketDidReceiveMsg:(NSNotification *)note {

    [_chart_Cell.stockChartView.stockChartView reloadData];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setProperty];
    [self cereateUI];
    [self sendData];
    
}

-(void)setProperty{
    
    _sectionMarr = [[NSMutableArray alloc]init];

    self.type = 1;
    CellModel* model0 =[[CellModel alloc]init];
    model0.reuseIdentifier = NSStringFromClass([WKStockHeaderTableViewCell class]);
    model0.className=NSStringFromClass([WKStockHeaderTableViewCell class]);
    model0.height = [WKStockHeaderTableViewCell computeHeight:@""];
    model0.selectionStyle=UITableViewCellSelectionStyleNone;
    model0.accessoryType=UITableViewCellAccessoryNone;
    /*
     * 传递参数
     */
    model0.userInfo = @"";
    
    SectionModel *model_0=[SectionModel sectionModelWithTitle:@"" cells:@[model0]];
    model_0.headerhHeight=13;
    model_0.footerHeight=0.0001;
    [_sectionMarr addObject:model_0];
    
    
    CellModel* model1 =[[CellModel alloc]init];
    model1.reuseIdentifier = NSStringFromClass([WKStockChartTableViewCell class]);
    model1.className=NSStringFromClass([WKStockChartTableViewCell class]);
    model1.height = [WKStockChartTableViewCell computeHeight:@""];
    model1.selectionStyle=UITableViewCellSelectionStyleNone;
    model1.accessoryType=UITableViewCellAccessoryNone;
    /*
     * 传递参数
     */
    model1.userInfo = @"";
    SectionModel *model_1=[SectionModel sectionModelWithTitle:@"" cells:@[model1]];
    model_1.headerhHeight=10;
    model_1.footerHeight=[WKStockChartTableViewCell computeHeight:@""];
    [_sectionMarr addObject:model_1];
    
}

-(void)cereateUI{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = @"K线图";
    
    [self.bgView addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(0);
        make.left.equalTo(self.bgView).offset(0);
        make.right.equalTo(self.bgView).offset(0);
        make.bottom.equalTo(self.bgView).offset(-50);
    }];
    
   

    [_tableView reloadData];
    WS(weakSelf);
    [self.bgView addSubview:self.footerView];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.equalTo(self.bgView).offset(0);
        make.right.equalTo(self.bgView).offset(0);
        make.bottom.equalTo(self.bgView).offset(-10);
    }];
    self.footerView.block = ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
}


#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionMarr.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section<2) {
        SectionModel *sm = _sectionMarr[section];
        return sm.cells.count;
    }else{
        if (self.type==1) {
            return 10;
        }else if (self.type==2){
            return 5;
        }else{
            return 1;
        }
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section<2) {
        SectionModel *sm = _sectionMarr[section];
        return sm.headerhHeight;
    }else{
        return [WKStock_Chart_Detail_Type_Section_View computeHeight:self.type];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section <2 ) {
        SectionModel *sm = _sectionMarr[section];
        return sm.footerHeight;
    }else{
        return 50;
    }

    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section<2) //价格分区视图
    {
        return nil;
    }
    else //成交、 简介分区视图
    {
        WKStock_Chart_Detail_Type_Section_View *type_SectionView=[tableView dequeueReusableHeaderFooterViewWithIdentifier:type_SectionID];
        
        WS(weakSelf);
        
        type_SectionView.itemBlock = ^(NSInteger type) {
            
            weakSelf.type=type;
            [weakSelf.tableView reloadData];
//            weakSelf.tableView.mj_footer.hidden = weakSelf.type == 2;
//            [weakSelf.tableView reloadData];
        };
        
        [type_SectionView relayOutUIWithType:self.type];
        
        return type_SectionView;
    }
    
}
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    return nil;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section <2 ) {
        SectionModel *sm = _sectionMarr[indexPath.section];
        CellModel *cm = sm.cells[indexPath.row];
        return cm.height;
    }else{
        if (self.type==1) {
           return [WKStockDepthTableViewCell computeHeight:@""];
        }else if (self.type==2){
           return [WKStockDealTableViewCell computeHeight:@""];
        }else{
           return [WKStockIntrolTableViewCell computeHeight:@""];
        }
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section <2 ) {
        SectionModel *sm = _sectionMarr[indexPath.section];
        CellModel *cm = sm.cells[indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
        if (!cell) {
            cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
        }
        cell.selectionStyle = cm.selectionStyle;
        
        if ([cm.className isEqualToString:NSStringFromClass([WKStockChartTableViewCell class])]) {
            WKStockChartTableViewCell *ex_cell=(WKStockChartTableViewCell *)cell;
            _chart_Cell = ex_cell;
            //        ex_cell.dataSource=cm.userInfo;
            
        }
        if ([cm.className isEqualToString:NSStringFromClass([WKStockHeaderTableViewCell class])]) {
            WKStockHeaderTableViewCell *ex_cell=(WKStockHeaderTableViewCell *)cell;
            //        [ex_cell setTitleLabelText:self.SystemType.Name];
            //        ex_cell.dataSource=cm.userInfo;
            //        ex_cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        //深度
        if (self.type == 1) {
            NSString *reuseIdentifier =[NSString stringWithFormat:@"WKStockDepthTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            
            WKStockDepthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[WKStockDepthTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        //成交
        }else if (self.type == 1){
            NSString *reuseIdentifier =[NSString stringWithFormat:@"WKStockDealTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            WKStockDealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[WKStockDealTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        //简介
        }else{
            NSString *reuseIdentifier =[NSString stringWithFormat:@"WKStockIntrolTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            WKStockIntrolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[WKStockIntrolTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of  nmthat can be recreated.
}


-(void)sendData{
    
    SocketRocketUtility * SK =[SocketRocketUtility instance];
    HomeBaseModel * Hmodel =[HomeBaseModel sharedHomeBaseModel];
    
    NSString * args1 = [NSString stringWithFormat:@"history.L20.%@",Hmodel.selectedModel.symbol];
    NSData *data1= [NSJSONSerialization dataWithJSONObject:@{
                                                             @"cmd": @"sub",
                                                             @"args": args1
                                                             } options:NSJSONWritingPrettyPrinted error:nil];
    [SK sendData:data1];
    NSData *data2= [NSJSONSerialization dataWithJSONObject:@{
                                                             @"cmd": @"sub",
                                                             @"args":
                                                                 @{
                                                                     @"type":[NSString stringWithFormat:@"kline.M60.%@",Hmodel.selectedModel.symbol],
                                                                     @"from":[K_LineDataModel Get_D1_TimeInterval],
                                                                     @"to":[K_LineDataModel Get_Now_TimeInterval]
                                                                     
                                                                     }
                                                             } options:NSJSONWritingPrettyPrinted error:nil];
    [SK sendData:data2];
}

-(WKStockFooterView *)footerView{
    if (!_footerView) {
        _footerView = [[WKStockFooterView alloc]init];
    }
    return _footerView;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WKStock_Chart_Detail_Type_Section_View class] forHeaderFooterViewReuseIdentifier:type_SectionID];

    }
    return _tableView;
}

@end











